import nltk
from nltk.featstruct import FeatStruct
from nltk.draw.tree import *

from rai.tagger.sentenceparser import *
import rai.file


def guiSentence(line):
    parser = SentenceParser()
    parsed_sentence = parser.parseSentence(line)
    #parsedSentences.append(parsed_sentence)
    #world.addFact(parsed_sentence)
    print parsed_sentence
    
    sentree = nltk.tree.Tree("S", children=[])
    for part in parsed_sentence:
        # first element is the type
        # the following element are tuples
        childTree = nltk.tree.Tree(part[0], children=[]) 
        sentree.append(childTree)
        #tree.children.append(childTree)
        for taggedpart in part[1:]:
            # '([cat=NP] [lex=the] [lex=dog])'
            #tagNode = nltk.tree.Tree(str(taggedpart), children=[])
            #tagNode = nltk.tree.Tree.parse('([cat=NP] [lex=the] [lex=dog])',parse_node=FeatStruct, parse_leaf=FeatStruct)
            stringval = "(%s[brown='%s',conll='%s'])" % (taggedpart[0], taggedpart[1], taggedpart[2])
            #tagNode = nltk.tree.Tree.parse(stringval,parse_node=FeatStruct, parse_leaf=FeatStruct)
            tagNode = nltk.tree.Tree.parse(stringval,parse_node=FeatStruct)
            #tagNode = nltk.tree.Tree('([cat=NP] [lex=the] [lex=dog])')
            childTree.append(tagNode)
    
    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), sentree, draggable=1,  
                  #node_font=('helvetica', -14, 'bold'), 
                  #leaf_font=('helvetica', -12, 'italic'), 
                  #roof_fill='white', roof_color='black', 
                  leaf_color='green4', node_color='blue2',
                  xspace=10,yspace=10) 
    cf.add_widget(tc,10,10) 
    cf.mainloop()
    print "Succes!"
    
def ReadFile():
    __dir__ = rai.file.__path__
    file_location = __dir__[0] + "\\test.facts.txt"
    f = open(file_location, 'r')

    exceptions = []
    max = 1
    i = 0
    lastLine = ""
    #while False:
    #while line != "":
    line = f.readline()
    while i < max:
        full=True
        #parsed_sentence = parser.parseSentence(line)
        guiSentence(line)
        #s [['VP', 'to'], ['VP', 'have', 'given']]
        #s [['VP', ('to', 'TO', 'TO')], ['VP', ('have', 'HV', 'VBP'), ('given', 'VBN', 'VBN')]]
        lastLine = line   
        line = f.readline()
        i = i + 1    

ReadFile()